package com.link.play.fit.ai.ui.dialog

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Dialog
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.FrameLayout
import com.link.play.fit.ai.R
import com.link.play.fit.ai.bean.CalendarStatusData
import com.link.play.fit.ai.databinding.HomeStreakDialogLayoutBinding
import com.link.play.fit.ai.ext.d
import com.link.play.fit.ai.utils.CalendarUtils.getWeekDateRange
import com.link.play.fit.ai.utils.CalendarUtils.weekParseDate
import com.link.play.fit.ai.utils.WeekDatesUtil.getDayOfWeek
import com.link.play.fit.ai.utils.WeekDatesUtil.getWeekday

class HomeStreakDialog(context: Activity, val confirmOnclick: () -> Unit) {
    private var mContext: Activity = context
    private var dialog: Dialog? = null
    private lateinit var itemBinding: HomeStreakDialogLayoutBinding
    private var clockList: MutableList<CalendarStatusData> = mutableListOf()
    private var latestList: MutableList<CalendarStatusData> = mutableListOf()

    private var mondayData: CalendarStatusData? = null
    private var tuesdayData: CalendarStatusData? = null
    private var wednesdayData: CalendarStatusData? = null
    private var thursdayData: CalendarStatusData? = null
    private var fridayData: CalendarStatusData? = null
    private var saturdayData: CalendarStatusData? = null
    private var sundayData: CalendarStatusData? = null

    @SuppressLint("ClickableViewAccessibility", "InflateParams", "SuspiciousIndentation")
    fun builder(): Dialog {

        val view: View =
            LayoutInflater.from(mContext).inflate(R.layout.home_streak_dialog_layout, null)
        itemBinding = HomeStreakDialogLayoutBinding.bind(view)
        dialog = Dialog(mContext)
        dialog?.setContentView(view)
        val window: Window? = dialog?.window
        window?.run {
            setBackgroundDrawable(ColorDrawable())
            setDimAmount(0.4f)
            setGravity(Gravity.CENTER)
            val layoutParams = attributes
            //设置dialog可以穿透并且有蒙层
            layoutParams.flags =
                    //WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
                WindowManager.LayoutParams.FLAG_DIM_BEHIND
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
//            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
            // layoutParams.verticalMargin = 0.06f
            attributes = layoutParams
            decorView.setPadding(0, 0, 0, 0)
        }
        itemBinding.contentLayout.setOnTouchListener { _, event ->

            val height: Int = itemBinding.popLayout.top
            val y = event.y
            if (event.action == MotionEvent.ACTION_UP) {
                if (y < height) {
//                    dialog?.dismiss()
                    //获取本周7天的日期，格式为"2025-04-01",并且返回7天的星期首字母
                }
            }
            true
        }

        itemBinding.continueText.setOnClickListener { dialog?.dismiss() }
        when (getDayOfWeek()) {
            1 -> {
                itemBinding.MondayText.setTextColor(mContext.getColor(R.color.color_17CB81))
            }
            2 -> {
                itemBinding.TuesdayText.setTextColor(mContext.getColor(R.color.color_17CB81))

            }
            3 -> {
                itemBinding.WednesdayText.setTextColor(mContext.getColor(R.color.color_17CB81))
            }
            4 -> {
                itemBinding.ThursdayText.setTextColor(mContext.getColor(R.color.color_17CB81))
            }

            5 -> {
                itemBinding.FridayText.setTextColor(mContext.getColor(R.color.color_17CB81))
            }
            6 -> {
                itemBinding.SaturdayText.setTextColor(mContext.getColor(R.color.color_17CB81))
            }
            7 -> {
                itemBinding.SundayText.setTextColor(mContext.getColor(R.color.color_17CB81))
            }
        }
        //
        latestList.forEachIndexed { _, calendarStatusData ->
            val firstM = getWeekday(calendarStatusData.timeDate, context = mContext)
            when (firstM) {
                mContext.getString(R.string.Monday) -> {
                    mondayData = calendarStatusData
                }

                mContext.getString(R.string.Tuesday) -> {
                    tuesdayData = calendarStatusData
                }

                mContext.getString(R.string.Wednesday) -> {
                    wednesdayData = calendarStatusData
                }

                mContext.getString(R.string.Thursday) -> {
                    thursdayData = calendarStatusData
                }

                mContext.getString(R.string.Friday) -> {
                    fridayData = calendarStatusData
                }

                mContext.getString(R.string.Saturday) -> {
                    saturdayData = calendarStatusData
                }

                mContext.getString(R.string.Sunday) -> {
                    sundayData = calendarStatusData
                }
            }
        }
        var size = 0
        clockList.forEachIndexed { _, calendarStatusData ->
            if (calendarStatusData.consumeCaloriesDaily > 0 || calendarStatusData.consumeProteinDaily > 0 || calendarStatusData.consumeCarbsDaily > 0 || calendarStatusData.consumeFatsDaily > 0) {
                size += 1
            }
        }
        itemBinding.homeRightText.text = "$size"
        itemBinding.numText.text = "$size"

        mondayData?.let {
            if (it.consumeCaloriesDaily > 0 || it.consumeFatsDaily>0 || it.consumeCarbsDaily>0 || it.consumeProteinDaily>0) {
                itemBinding.MondayValue.setImageResource(R.mipmap.dak_chose_bg)
            }
        }
        tuesdayData?.let {
            if (it.consumeCaloriesDaily > 0 || it.consumeFatsDaily>0 || it.consumeCarbsDaily>0 || it.consumeProteinDaily>0) {
                itemBinding.TuesdayValue.setImageResource(R.mipmap.dak_chose_bg)
            }
        }
        wednesdayData?.let {
            if (it.consumeCaloriesDaily > 0 || it.consumeFatsDaily>0 || it.consumeCarbsDaily>0 || it.consumeProteinDaily>0) {
                itemBinding.WednesdayValue.setImageResource(R.mipmap.dak_chose_bg)
            }
        }
        thursdayData?.let {
            if (it.consumeCaloriesDaily > 0 || it.consumeFatsDaily>0 || it.consumeCarbsDaily>0 || it.consumeProteinDaily>0) {
                itemBinding.FridayValue.setImageResource(R.mipmap.dak_chose_bg)
            }
        }
        fridayData?.let {
            if (it.consumeCaloriesDaily > 0 || it.consumeFatsDaily>0 || it.consumeCarbsDaily>0 || it.consumeProteinDaily>0) {
                itemBinding.MondayValue.setImageResource(R.mipmap.dak_chose_bg)
            }
        }
        saturdayData?.let {
            if (it.consumeCaloriesDaily > 0 || it.consumeFatsDaily>0 || it.consumeCarbsDaily>0 || it.consumeProteinDaily>0) {
                itemBinding.SaturdayValue.setImageResource(R.mipmap.dak_chose_bg)
            }
        }
        sundayData?.let {
            if (it.consumeCaloriesDaily > 0 || it.consumeFatsDaily>0 || it.consumeCarbsDaily>0 || it.consumeProteinDaily>0) {
                itemBinding.SundayView.setImageResource(R.mipmap.dak_chose_bg)
            }
        }
        return dialog!!
    }

    fun setData(list: MutableList<CalendarStatusData>) {
        clockList = list
//        latestList = clockList.takeLast(7).toMutableList()
        latestList = clockList.filter { item ->
            val (start, end) = getWeekDateRange()
            val itemTime = weekParseDate(item.timeDate)
            itemTime in start..end
        }.toMutableList()
        "filteredList  $latestList".d()
    }

//    fun experienceNowOnclick(listener: View.OnClickListener): HomeStreakDialog {
//        itemBinding.experienceNow.setOnClickListener { v ->
//            listener.onClick(v)
//            dialog?.dismiss()
//        }
//        return this
//    }
//    fun exitOnclick(listener: View.OnClickListener): HomeStreakDialog {
//        itemBinding.exit.setOnClickListener { v ->
//            listener.onClick(v)
//            dialog?.dismiss()
//        }
//        return this
//    }

}